package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.UserData;
import com.ruoyi.system.service.IUserDataService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户关联表Controller
 * 
 * @author ruoyi
 * @date 2020-03-29
 */
@Controller
@RequestMapping("/system/userdata")
public class UserDataController extends BaseController
{
    private String prefix = "system/userdata";


    @Autowired
    private IUserDataService userDataService;

    @RequiresPermissions("system:userdata:view")
    @GetMapping()
    public String userdata()
    {
        return prefix + "/userdata";
    }

    /**
     * 查询用户关联表列表
     */
    @RequiresPermissions("system:userdata:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserData userData)
    {
        startPage();
        List<UserData> list = userDataService.selectUserDataList(userData);
        return getDataTable(list);
    }

    /**
     * 导出用户关联表列表
     */
    @RequiresPermissions("system:userdata:export")
    @Log(title = "用户关联表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserData userData)
    {
        List<UserData> list = userDataService.selectUserDataList(userData);
        ExcelUtil<UserData> util = new ExcelUtil<UserData>(UserData.class);
        return util.exportExcel(list, "userdata");
    }

    /**
     * 新增用户关联表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户关联表
     */
    @RequiresPermissions("system:userdata:add")
    @Log(title = "用户关联表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserData userData)
    {
        return toAjax(userDataService.insertUserData(userData));
    }

    /**
     * 修改用户关联表
     */
    @GetMapping("/edit/{uId}")
    public String edit(@PathVariable("uId") Long uId, ModelMap mmap)
    {
        UserData userData = userDataService.selectUserDataById(uId);
        mmap.put("userData", userData);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户关联表
     */
    @RequiresPermissions("system:userdata:edit")
    @Log(title = "用户关联表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserData userData)
    {
        return toAjax(userDataService.updateUserData(userData));
    }

    /**
     * 删除用户关联表
     */
    @RequiresPermissions("system:userdata:remove")
    @Log(title = "用户关联表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)

    {
        return toAjax(userDataService.deleteUserDataByIds(ids));
    }

    /**
     * 导入Excel文件
     */
    /*@PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<WeixinData> util = new ExcelUtil<WeixinData>(SysUser.class);
        List<SysUser> userList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = userService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }*/
}
